package com.ltsznh.android.frequencycounter;

import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by 涛 on 13-12-24.
 */
public class TouchView extends SurfaceView implements SurfaceHolder.Callback {
    protected TouchThread touchThread;//
    private Handler handler = new Handler( );

    private int cHeight;
    private int cWdith;

    public TouchView(Context context, AttributeSet attrs,int dd) {
        super(context, attrs, dd);
        init();
    }

    public TouchView(Context context, AttributeSet attrs) {
        super(context,attrs);
        init();
    }

    public TouchView(Context context) {
        super(context);
        init();
    }

    protected void init(){
        SurfaceHolder surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);

        touchThread = new TouchThread(surfaceHolder);
        touchThread.start();



    }

    private Runnable runnable = new Runnable( ) {
        public void run ( ) {
            touchThread.drawCircle();
            handler.postDelayed(this,50);
//postDelayed(this,1000)方法安排一个Runnable对象到主线程队列中
        }
    };

//    handler.postDelayed(runnable,1000); // 开始Timer
//    handler.removeCallbacks(runnable); //停止Timer

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        cWdith = getWidth();
        cHeight = getHeight();
        touchThread.setSize(cWdith,cHeight);
        handler.postDelayed(runnable,1000); // 开始Timer

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
        touchThread.run();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        handler.removeCallbacks(runnable); //停止Timer
    }



}
